
$("#menu-toggle").click(function(e) {
    e.preventDefault();
    $("#page-container").toggleClass("toggled");
});

var notices = [];
var global_notice = [];

var refresh = function() {
	var postData = {
		notices: notices.toString()
	};
	/*$.ajax({
		url: '/application/notice',
		type: 'POST',
		data: postData,
		success: function(data) {
			for (var i = 0; i < data.length; i++) {
				notices.push(data[i].updates_id);
				var string = data[i].update;
				if(string.length > 25) {
					data[i].update = string.substring(0,150)+"...";
				}
				notification(data[i]);
			}
		}
	});*/
	$.ajax({
		url: '/application/globalnotice',
		type: 'POST',
		data: {notices: global_notice.toString()},
		success: function(data) {
			for (var i = 0; i < data.length; i++) {
				global_notice.push(data[i].updates_id);
				globalnotice(data[i]);
			}
			if(global_notice.length > 0){
				//alert(data.length);
				$('#header_globe span.label').html(data.length);
				$('#header_globe span.label').show();
			}
		}
	});
}
setInterval(refresh, 3000);
    
function notification(data){
	setTimeout(function() {
	    $.gritter.add({
	        title: "",
	        text: data.update,
	        image: data.photo,
	        sticky: true,
	        time: "",
	        class_name: "my-sticky-class"
	    })
	}, 100);
}

function globalnotice(data){
	//console.log(data);
	if (data.type == 0 ){
		var html = $('<li class="media"></li>');
		var ahref = $('<a href="/profile/sharejourney?user_id='+data.user_id+'&journey_id='+data.type_id+'&share_id='+data.share_id+'"></a>');
		var a_divpull = $('<div class="pull-left"></div>');
		var a_divpull_img = $('<img src="'+data.photo+'" class="media-object" alt="">');
		var a_divboby = $('<div class="media-body"><div>');
		var a_divbody_h6 = $('<h6 class="media-heading">'+data.firstname +' ' + data.lastname + '<h6>');
		var a_divbody_p = $('<p>'+data.name+'</p>');
		a_divpull.append(a_divpull_img);
		a_divboby.append(a_divbody_h6);
		a_divboby.append(a_divbody_p);
		ahref.append(a_divpull);
		ahref.append(a_divboby);
		html.append(ahref);
		$('#header_globe ul').append(html);
	} else if (data.type==1){
		
		var html = $('<li class="media"></li>');
		var ahref = $('<a href="/profile/shareposition?user_id='+data.user_id+'&position_id='+data.type_id+'&share_id='+data.share_id+'"></a>');
		var a_divpull = $('<div class="pull-left"></div>');
		var a_divpull_img = $('<img src="'+data.photo+'" class="media-object" alt="">');
		var a_divboby = $('<div class="media-body"><div>');
		var a_divbody_h6 = $('<h6 class="media-heading">'+data.firstname +' ' + data.lastname + '<h6>');
		var a_divbody_p = $('<p>'+data.name+'</p>');
		a_divpull.append(a_divpull_img);
		a_divboby.append(a_divbody_h6);
		a_divboby.append(a_divbody_p);
		ahref.append(a_divpull);
		ahref.append(a_divboby);
		html.append(ahref);
		$('#header_globe ul').append(html);
	} else  {
		var conf = confirm(data.firstname+' '+data.lastname + 'Sent you a friend request !! Are you ok ?');
		if (conf == true ) {
			$.ajax({
				type: "POST",
				url: '/application/friendRequest',
				data :{
					share_id: data.share_id,
					user_id : data.user_id,
					flag_cancel : 0 
				},
				success: function(data) {
					
				}
				});
		}else {
				$.ajax({
					type: "POST",
					url: '/application/friendRequest',
					data :{
						share_id: data.share_id,
						user_id : data.user_id,
						flag_cancel : 1
					},
					success: function(data) {
						
					}
					});
		}
		
		
	}
	
	
	
}
function addRequest(data){
	//alert (user_id);
	var html = data.firstname+' '+data.lastname + 'Sent you a friend request !! Are you ok ?'; 
	alert (html);
	
}